package cn.pzaily.fealetter.config;

import cn.pzaily.fealetter.listener.MyRedisSubscriberListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @PackageName: cn.pzaily.fealetter.config
 * @Description:
 * @Author: Pang
 * @Date: 2021-11-20 14:49
 */
@Configuration
@AutoConfigureAfter({MyRedisSubscriberListener.class})
public class SubscriberConfig {

    @Autowired
    private MyRedisSubscriberListener myRedisSubscriberListener;

    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);

        redisMessageListenerContainer.addMessageListener(
                new MessageListenerAdapter(myRedisSubscriberListener), new PatternTopic("MYTOPIC"));

        return redisMessageListenerContainer;
    }

}
